package com.study;

import org.junit.Test;

import java.sql.*;

public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        method01();
    }
    public static void method04() throws ClassNotFoundException, SQLException {
        //注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获得连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/kkb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "root");
        //获得执行sql语句的对象
        Statement statement = conn.createStatement();
        String sql = "delete from tb_category where cid = 5";
        //执行sql语句，并返回结果
        int i = statement.executeUpdate(sql);
        //处理结果
        System.out.println("受影响的行数是" + i);
        //释放资源
        statement.close();
        conn.close();
    }

    public static void  method03() throws ClassNotFoundException, SQLException {
        //注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获得连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/kkb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "root");
        //获得执行sql语句的对象
        Statement statement = conn.createStatement();
        String sql = "update tb_category set cname = '浴霸' where cid = 5";
        //执行sql语句，并返回结果
        int i = statement.executeUpdate(sql);
        //处理结果
        System.out.println("受影响的行数是" + i);
        //释放资源
        statement.close();
        conn.close();
    }

    public static void method02() throws ClassNotFoundException, SQLException {
        // 注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 获得链接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/kkb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "root");
        // 创建对象执行sql语句
        Statement statement = conn.createStatement();
        String sql = "insert into tb_category values(null, '卫浴')";
        // 执行sql
        int num = statement.executeUpdate(sql);
        // 处理结果.
        System.out.println("受影响的行: " + num);
        // 释放资源
        statement.close();
        conn.close();
    }

    public static void method01() throws ClassNotFoundException, SQLException {
        // 注册驱动，加载mysql驱动class文件。 有了这一步操作，Java可以链接mysql了
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 获得链接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/kkb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "root");
        // 创建一个对象，作用是执行SQL
        Statement statement = conn.createStatement();
        String sql = "SELECT * FROM tb_category";
        // 执行sql语句并记录返回结果
        ResultSet rs = statement.executeQuery(sql);
        // 处理返回结果，打印输出
        while (rs.next()){
            int cid = rs.getInt("cid");
            String cname = rs.getString("cname");
            System.out.println(cid + "----" + cname);
        }
        // 释放资源
        rs.close();
        statement.close();
        conn.close();
    }
}
